package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.a931xmc.yuejian20.Bean.ActivityBean;
import cst.a931xmc.yuejian20.Bean.AppBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ActivityModel;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.R;

public class ActivityAppActivity extends AppCompatActivity implements View.OnClickListener {
    private Context context;
    private int activity_id;
    private int user_id;
    private int activity_appnum;
    private ImageView activity_pic;
    private TextView activity_name;
    private TextView activity_price;
    private TextView activity_appnotice;
    private EditText aorder_rname;
    private EditText aorder_tel;
    private EditText aorder_idcard;
    private EditText aorder_num;
    private EditText aorder_price;
    private ImageButton btn_back;
    private ImageButton btn_off;
    private ImageButton btn_add;
    private Button btn_pay;
    private ActivityModel model;
    private Listener<AppBean> listener;
    private Listener<ActivityBean> listener1;
    private int per;
    private String activityname;
    private String activitypic;
    private String activityintro;
    SharedPreferences sp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activityapp);
        init();
        initData();
        model = new ActivityModel();
        model.getDetail(activity_id,listener1);
    }

    public void init(){
        activity_pic = findViewById(R.id.activity_pic);
        activity_name = findViewById(R.id.activity_name);
        activity_price = findViewById(R.id.activity_price);
        activity_appnotice = findViewById(R.id.activity_appnotice);
        aorder_rname = findViewById(R.id.aorder_rname);
        aorder_tel = findViewById(R.id.aorder_tel);
        aorder_idcard = findViewById(R.id.aorder_idcard);
        aorder_num = findViewById(R.id.aorder_num);
        aorder_price = findViewById(R.id.aorder_price);
        btn_back = findViewById(R.id.btn_back);
        btn_off = findViewById(R.id.btn_off);
        btn_add = findViewById(R.id.btn_add);
        btn_pay = findViewById(R.id.btn_pay);
        btn_back.setOnClickListener(this);
        btn_off.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        btn_pay.setOnClickListener(this);
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        Intent intent = getIntent();
        activity_id = intent.getExtras().getInt("activity_id");
        user_id = sp.getInt("user_id",0);
    }

    public void initData(){
        listener1 = new Listener<ActivityBean>() {
            @Override
            public void onResponse(ActivityBean l) {
                if(l.getActivity_id()!=0){
                    activity_name.setText(l.getActivity_name());
                    activity_price.setText("￥" + l.getActivity_price() + "/人次");
                    activity_appnotice.setText(l.getActivity_appnotice());
                    activity_appnum = Integer.parseInt(l.getActivity_appnum());
                    activityname = l.getActivity_name();
                    activitypic = l.getActivity_pic();
                    activityintro = l.getActivity_intro();
                    per = Integer.parseInt(l.getActivity_price());
                    aorder_price.setText(l.getActivity_price());
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getActivity_pic())
                            .placeholder(R.drawable.nopic)
                            .into(activity_pic);
                }
                else {
                    Toast.makeText(ActivityAppActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(ActivityAppActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };

        listener = new Listener<AppBean>() {
            @Override
            public void onResponse(AppBean l) {
                if(l.getOrder_id()!=0){
                    Intent intent = new Intent(ActivityAppActivity.this,SuccessActivity.class);
                    intent.putExtra("aorder_id",l.getOrder_id());
                    startActivity(intent);
                    finish();
                    Toast.makeText(ActivityAppActivity.this, "报名成功", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(ActivityAppActivity.this, "报名失败", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(ActivityAppActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };
    }

    @Override
    public void onClick(View v) {
        int num = Integer.parseInt(aorder_num.getText().toString());
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_off:
                if(num!=1){
                    aorder_num.setText(Integer.toString(--num));
                }
                else {
                    Toast.makeText(ActivityAppActivity.this, "报名数量至少为1", Toast.LENGTH_LONG).show();
                }
                aorder_price.setText(Integer.toString(per*num));
                break;
            case R.id.btn_add:
                if(num!=10){
                    aorder_num.setText(Integer.toString(++num));
                }
                else {
                    Toast.makeText(ActivityAppActivity.this, "报名数量上限为10", Toast.LENGTH_LONG).show();
                }
                aorder_price.setText(Integer.toString(per*num));
                break;
            case R.id.btn_pay:

                if(TextUtils.isEmpty(aorder_rname.getText().toString())){
                    Toast.makeText(ActivityAppActivity.this, "姓名不能为空", Toast.LENGTH_LONG).show();
                }
                else if(TextUtils.isEmpty(aorder_tel.getText().toString())){
                    Toast.makeText(ActivityAppActivity.this, "电话不能为空", Toast.LENGTH_LONG).show();
                }
                else if(TextUtils.isEmpty(aorder_idcard.getText().toString())){
                    Toast.makeText(ActivityAppActivity.this, "身份证号不能为空", Toast.LENGTH_LONG).show();
                }
                else {
                    model = new ActivityModel();
                    model.order(activity_id,activityname,activitypic,activityintro,user_id,
                            aorder_rname.getText().toString(),
                            aorder_tel.getText().toString(),
                            aorder_idcard.getText().toString(),
                            Integer.parseInt(aorder_num.getText().toString()),
                            aorder_price.getText().toString(),
                            listener);
                    activity_appnum += Integer.parseInt(aorder_num.getText().toString());
                    model.updateActivityAppNum(activity_id,activity_appnum);
                }
        }
    }
}
